package com.luyou.module.system.controller.admin.auth;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import com.luyou.framework.common.domain.Result;
import com.luyou.framework.security.core.LoginUser;
import com.luyou.module.system.controller.admin.auth.vo.AuthLoginReqVO;
import com.luyou.module.system.controller.admin.auth.vo.AuthLoginRespVO;
import com.luyou.module.system.service.auth.AuthUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author luyou
 * @version 1.0
 * @date 2024/6/30 18:12
 * @desc
 */
@Slf4j
@Tag(name = "管理后台 - 认证")
@RequestMapping("/system/auth")
@RestController
public class AuthController {

    @Resource
    private AuthUserService authUserService;

    @SaIgnore
    @PostMapping("/login")
    @Operation(summary = "使用账号密码登录")
    public Result<AuthLoginRespVO> login(@RequestBody @Valid AuthLoginReqVO reqVO) {
        LoginUser loginUser = authUserService.login(reqVO.getUsername(), reqVO.getPassword());
        StpUtil.login(loginUser.getId());
        String accessToken = StpUtil.getTokenValue();
        return Result.success(AuthLoginRespVO.builder().accessToken(accessToken).build());
    }

    @SaCheckLogin
    @PostMapping("/getUserInfo")
    public Result<String> getUserInfo() {
        return Result.success(String.valueOf(StpUtil.getLoginId()));
    }
}
